﻿using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.Messaging;
using FileTransferDesktop.Message;
using FileTransferDesktop.Services;

namespace FileTransferDesktop.ViewModels.Pages;

public partial class RecycleBinItem : ObservableObject
{
    [ObservableProperty]
    private string _fileName = "";

    [ObservableProperty]
    private string _filePath = "";

    [ObservableProperty]
    private string _message = "";

    [ObservableProperty]
    private DateTime _originalTime;

    [ObservableProperty]
    private DateTime _deletedAt;

    [ObservableProperty]
    private string _type = "";

    [ObservableProperty]
    private string _shareCode = "";
}

public partial class RecycleBinViewModel : ObservableObject, IRecipient<RecycleBinChangedMessage>
{
    [ObservableProperty]
    private ObservableCollection<RecycleBinItem> _items = [];

    public RecycleBinViewModel()
    {
        LoadRecycleRecords();
        WeakReferenceMessenger.Default.Register<RecycleBinChangedMessage>(this);
    }

    public void Receive(RecycleBinChangedMessage message)
    {
        LoadRecycleRecords();
    }

    public void LoadRecycleRecords()
    {
        var records = LocalDbService.GetRecycleRecords();
        Items = new ObservableCollection<RecycleBinItem>(
            records.Select(r => new RecycleBinItem
            {
                FileName = r.FileName,
                FilePath = r.FilePath,
                Message = r.Message,
                OriginalTime = r.OriginalTime,
                DeletedAt = r.DeletedAt,
                Type = r.Type,
                ShareCode = r.ShareCode,
            })
        );
    }

    [RelayCommand]
    private void ClearRecycle()
    {
        LocalDbService.ClearRecycleBin();
        Items.Clear();
    }
}
